-
Notifications
You must be signed in to change notification settings - Fork 863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix SSE client handling of nested path URLs #386
base: main
Are you sure you want to change the base?
Conversation
Fix issue with SSE client failing when connecting to servers with nested paths (e.g., http://domain/path1/path2/sse). Previously, the client worked only with simple URLs like http://domain/sse. The fix extracts the base path segment between domain and /sse endpoint using regex, then correctly constructs the response URL by preserving the path structure. This ensures proper communication with MCP servers hosted under non-root paths. Without this fix, clients couldn't connect to SSE servers deployed under nested paths, as the endpoint URLs for posting messages were constructed incorrectly.
Thank you very much @danilpavlov!!! You saved my day with your PR. I hope it will be merged soon. |
Nice! Just wanted to add that using this alongside langchain's adapters that use the
Please note that I also had to change the urljoin
|
Please see danilpavlov#1 @danilpavlov |
fixing /mcp case prefix on langchain
Thank you @jonathanglima so much for that feature! |
@dsp-ant Can you please check this PR. Forget to mark as reviewer :/ |
Be good to get this merged and released |
Thanks a lot for the great PR! I have a suggestion regarding the current implementation. Right now, the SSE client dynamically extracts the base path using regex to reconstruct the endpoint URL. While this works, it places too much responsibility on the client to figure out the correct path structure. This can lead to edge cases and inconsistent behavior—especially in environments with path-based routing like Kubernetes. A more reliable and maintainable approach would be to have the server explicitly provide the full and correct session_id = uuid4()
# === mount_prefix ===
request_path = scope["path"] # e.g., "/test/sse"
match = re.match(r"^/([^/]+(?:/mcp)?)/sse$", request_path)
mount_prefix = match.group(1) if match else ""
session_uri = f"/{quote(mount_prefix)}{quote(self._endpoint)}?session_id={session_id.hex}" If the server constructs the
|
That's nice @sebin1213 . I wasn't aware that the server could actually provide the session uri to the client. Responsibility-wise, that's much better |
Fix generate accurate session_uri for nested SSE paths
Yeah, @sebin1213 that's a really good point -- the server seem to be a better place for that feature. Great job! Thank you for joining us |
Fix SSE client failing when connecting to servers with nested paths (e.g., http://domain/path1/path2/sse). Previously, the client worked only with simple URLs like http://domain/sse.
The fix extracts the base path segment between domain and /sse endpoint using regex, then correctly constructs the response URL by preserving the path structure. This ensures proper communication with MCP servers hosted under non-root paths.
Without this fix, clients couldn't connect to SSE servers deployed under nested paths, as the endpoint URLs for posting messages were constructed incorrectly.
Motivation and Context
I got problem while deploying my MCP server inside a Kubernetes cluster. The SSE client was only working with simple URLs like http://example-domain/sse, but failed to properly handle more complex paths such as http://example-domain/path1/path2/sse.
When the server was deployed under nested paths, the client couldn't establish a proper connection because it was incorrectly constructing the endpoint URLs for posting messages. This made it impossible to use MCP servers hosted under non-root paths, which is a common scenario in Kubernetes environments where services often run behind path-based routing.
How Has This Been Tested?
I ve tested on version for local mcp server (http://localhost:8000/sse) and with special mask (http://domain/dummy/sse)
Types of changes
Checklist